<template>
  <div class="login-container">
    <div class="login-card">
      <h1 class="title">用户登录</h1>
      
      <el-form :model="form" :rules="rules" ref="loginForm" class="login-form" @keyup.enter="submitForm">
        <el-form-item label="用户名:" prop="userAccount" style="font-size: 16px; align-items: center;">
          <el-input
            v-model="form.userAccount"
            placeholder="用户名"
            class="input-with-icon"
          >
          <template #prefix>
            <el-icon class="prefix-icon">
              <User />
            </el-icon>
            <div class="icon-separator"></div>
          </template>
          </el-input>
        </el-form-item>

        <el-form-item label="密&nbsp&nbsp码:" prop="userPassword" style="font-size: 16px; align-items: center;">
          <el-input
            v-model="form.userPassword"
            type="password"
            placeholder="密码"
            class="input-with-icon"
            show-password
          >
            <template #prefix>
              <el-icon class="prefix-icon">
                <Key />
              </el-icon>
              <div class="icon-separator"></div>
            </template>
          </el-input>
        </el-form-item>

        <el-button
          type="primary"
          @click="submitForm"
          class="login-btn"
        >
          登录
        </el-button>
      </el-form>

      <div class="register-link">
        <span>没有账号？</span>
        <router-link to="/prior/regist">立即注册</router-link>
      </div>
    </div>
  </div>
</template>
  
<script setup>
  import { ref, reactive } from 'vue';
  import { useRouter } from 'vue-router';
  import { ElMessage } from 'element-plus';
  import { LoginServer } from '@/api/instance';
  import { ArrowLeftBold, User, Key, View , Hide} from '@element-plus/icons-vue'; 
  import { useUserStore } from '@/stores';

  const router = useRouter();
  const form = reactive({
      userAccount: '',
      userPassword: '',
  });

  const userStore = useUserStore();

  // 声明表单引用
  const loginForm = ref(null);

  const rules = {
      userAccount: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 1, max: 16, message: '长度1-16位', trigger: 'blur' },
      ],
      userPassword: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 8, message: '长度大于等于8位', trigger: 'blur' },
      ],
  };

  const submitForm = async () => {
    if (!loginForm.value) return;
    await loginForm.value.validate(async valid => {
      if (valid) {
        try {
          const res = await LoginServer(form);
          if (res.code === 0) {
              sessionStorage.setItem('token', res.data.jwt);
              userStore.changeUserRole(res.data.userRole);
              try {
                  router.replace({name: '主页面'});
                  ElMessage.success('登录成功');
                  form.userAccount = '';//清空
                  form.userPassword = '';
              } catch (error) {
                  console.error('跳转失败:', error); // 添加错误处理
              }
          } else {
            ElMessage.error(res.message || '登录失败');
          }
        } catch (error) {
          ElMessage.error(error.message ||'网络请求失败');
        }
      }else{
        ElMessage.error('输入格式不对');
      }
    });
  };
</script>

<style lang="scss" scoped>
span {
  font-family: '黑体', sans-serif; /* 全局字体 */
  font-weight: bold; /* 全局加粗 */
}

.login-container{
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: #f0f2f5;
  font-family: '黑体', sans-serif;
  font-weight: bold;
  .login-card{
    width: 400px;
    padding: 40px;
    background: white;
    border-radius: 10px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
    .title{
      margin: 20px 0px 30px 0px;
      font-size: 24px;
      color: #333;
      text-align: center;
    }
    .login-form{
      .input-with-icon{
        font-size: 16px;
        padding: 12px 20px;
        border-radius: 8px;
        width: 100%;
        .prefix-icon{
          margin-right: 5px; /* 图标和竖线之间的间距 */
        }
        .icon-separator{
          width: 1px;
          height: 14px; /* 竖线的高度 */
          background-color: #ccc; /* 竖线的颜色 */
          margin-right: 6px; /* 竖线和输入框内容之间的间距 */
        }
      }
    }
    .login-btn{
      font-size: 18px;
      padding: 12px 20px;
      margin-top: 10px;
      width: 100%;
      background: #409eff;
      border-color: #409eff;
      border-radius: 8px;
    }
    .register-link{
      margin-top: 20px;
      text-align: center;
      a{
        color: #409eff;
        text-decoration: underline;
      }
    }
  }
}
</style>